import { getPluginsList } from "./build/plugins";
import { include, exclude } from "./build/optimize";
import { type UserConfigExport, type ConfigEnv, loadEnv } from "vite";
import {
  root,
  alias,
  wrapperEnv,
  pathResolve,
  __APP_INFO__
} from "./build/utils";

export default ({ mode }: ConfigEnv): UserConfigExport => {
  const { VITE_CDN, VITE_PORT, VITE_COMPRESSION, VITE_PUBLIC_PATH } =
    wrapperEnv(loadEnv(mode, root));
  const define = {
    __INTLIFY_PROD_DEVTOOLS__: false,
    __APP_INFO__: JSON.stringify(__APP_INFO__)
  };

  return {
    base: VITE_PUBLIC_PATH,
    root,
    resolve: {
      alias
    },
    // 服务端渲染
    server: {
      // 端口号
      port: VITE_PORT,
      host: "0.0.0.0",
      // 本地跨域代理 https://cn.vitejs.dev/config/server-options.html#server-proxy
      proxy: {},
      // 预热文件以提前转换和缓存结果，降低启动期间的初始页面加载时长并防止转换瀑布
      warmup: {
        clientFiles: ["./index.html", "./src/{views,components}/*"]
      }
    },
    plugins: getPluginsList(VITE_CDN, VITE_COMPRESSION),
    // https://cn.vitejs.dev/config/dep-optimization-options.html#dep-optimization-options
    optimizeDeps: {
      include,
      exclude
    },
    build: {
      // https://cn.vitejs.dev/guide/build.html#browser-compatibility
      target: "es2015",
      sourcemap: true,
      // 消除打包大小超过500kb警告
      chunkSizeWarningLimit: 4000,
      lib: {
        entry: pathResolve("src/frame.ts", import.meta.url),
        name: "pure-admin-custom",
        fileName: "pure-admin-custom",
        formats: ["es"]
      },
      rollupOptions: {
        // 👇 这里配置 external
        external: [
          "vue",
          "vue-router",
          "vue-tippy",
          "vue-types",
          "pinia",
          "@vueuse/core",
          "@vueuse/motion",
          "axios",
          "qs",
          "element-plus",
          "pinyin-pro",
          "dayjs",
          "echarts",
          "js-cookie",
          "localforage",
          "mitt",
          "nprogress",
          "path-browserify",
          "responsive-storage",
          "sortablejs"
        ],
        output: {
          chunkFileNames: "js/[name]-[hash].js",
          // 👇 告诉 Rollup 这些外部模块在全局中的变量名（用于 UMD，可选）
          globals: {
            vue: "Vue",
            "vue-router": "VueRouter",
            "vue-tippy": "VueTippy",
            "vue-types": "VueTypes",
            pinia: "Pinia",
            "@vueuse/core": "VueUse",
            "@vueuse/motion": "VueUseMotion",
            axios: "axios",
            qs: "Qs",
            "element-plus": "ElementPlus",
            "pinyin-pro": "PinyinPro",
            dayjs: "dayjs",
            echarts: "echarts",
            "js-cookie": "Cookies",
            localforage: "localforage",
            mitt: "mitt",
            nprogress: "NProgress",
            "path-browserify": "path",
            "responsive-storage": "responsiveStorage",
            sortablejs: "Sortable"
          },
          manualChunks: undefined
        }
      }
    },
    define
  };
};
